﻿using System;
using System.Collections;
using System.Configuration;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;
using System.Web.UI.WebControls;


namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Cal_Price_Account : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_FiscalAccount d_fiscal = new D_FiscalAccount();
        M_FiscalAccount m_fiscal = new M_FiscalAccount();
        D_CalPriceAccount d_cal_price = new D_CalPriceAccount();
        M_CalPriceAccount m_cal_price = new M_CalPriceAccount();
        D_Asset d_asset = new D_Asset();
        M_Asset m_asset = new M_Asset();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hicm_cal_price_account_seq.Text = Request.QueryString["icm_cal_price_account_seq"].ToString();
                hStatus.Text = Request.QueryString["status"].ToString();
                EnableObject(true);
                if (hStatus.Text == "Edit")
                {
                    LoadToObject();
                    cmbAssetCode.Enabled = false;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (chkValidate())
            {
                try
                {
                    m_cal_price.asset_code = cmbAssetCode.SelectedValue.ToString();
                    m_cal_price.fiscal_period = cn.ConvertNullInt(cmbFiscal.SelectedValue);
                    m_cal_price.start_date_cal_depreciatiion = Convert.ToDateTime(txtStartDateCalDepre.TextWithLiterals);
                    m_cal_price.end_date_cal_depreciation = Convert.ToDateTime(txtEndDateCalDepre.TextWithLiterals);
                    m_cal_price.remark = txtRemark.Text;
                    m_cal_price.record_status = "U";
                    if (hStatus.Text == "Add")
                    {
                        m_cal_price.created_by = cn.UserID;
                        m_cal_price.created_when = DateTime.Now;
                        d_cal_price.Insert_CalPriceAccount(m_cal_price);
                        RadAjaxManager1.Alert("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    }
                    else if (hStatus.Text == "Edit")
                    {
                        m_cal_price.icm_cal_price_account_seq = Convert.ToInt32(hicm_cal_price_account_seq.Text);
                        m_cal_price.updated_by = cn.UserID;
                        m_cal_price.updated_when = DateTime.Now;
                        d_cal_price.Update_CalPriceAccount(m_cal_price);
                        RadAjaxManager1.Alert("แก้ไขข้อมูลเรียบร้อยแล้ว");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    EnableObject(false);
                    RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
                }
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (hStatus.Text != "Edit")
                ClearObject();
            else
                LoadToObject();
        }
        protected void cmbAssetCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_Asset> infoList = d_asset.GetListFillByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAssetCode.Items.Add(new RadComboBoxItem(infoList[i].asset_name, infoList[i].asset_code.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].asset_code;
                tdCode.Width = Unit.Pixel(200);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].asset_name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdDescr = new TableCell();
                Label lblDescr = new Label();
                lblDescr.Text = infoList[i].asset_remark;
                tdDescr.Width = Unit.Pixel(200);
                tdDescr.Controls.Add(lblDescr);
                tr.Cells.Add(tdDescr);
                ((Table)cmbAssetCode.Items[j].FindControl("tableAsset")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbFiscal_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_FiscalAccount> infoList = d_fiscal.GetListFilterByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbFiscal.Items.Add(new RadComboBoxItem(infoList[i].fiscal_name, infoList[i].fiscal_period.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        private void ClearObject()
        {
            cmbAssetCode.ClearSelection();
            cmbAssetCode.Text = "";
            cmbFiscal.ClearSelection();
            cmbFiscal.Text = "";
            txtStartDateCalDepre.Text = "";
            txtEndDateCalDepre.Text = "";
            txtRemark.Text = "";
        }
        private void EnableObject(bool pFlag)
        {
            cmbAssetCode.Enabled = pFlag;
            cmbFiscal.Enabled = pFlag;
            txtStartDateCalDepre.Enabled = pFlag;
            txtEndDateCalDepre.Enabled = pFlag;
            txtRemark.Enabled = pFlag;
            btnSave.Enabled = pFlag;
            btnCancel.Enabled = pFlag;
        }
        private bool chkValidate()
        {
            bool pFlag = true;
            if (cmbAssetCode.SelectedValue == "")
            {
                pFlag = false;
                cmbAssetCode.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกรหัสพัสดุที่จะบันทึกด้วย");
            }
            else if (cmbFiscal.SelectedValue == "")
            {
                pFlag = false;
                cmbFiscal.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกงวดบัญชี");
            }
            else if (txtStartDateCalDepre.Text == "")
            {
                pFlag = false;
                txtStartDateCalDepre.Focus();
                RadAjaxManager1.Alert("กรุณากรอกวันที่เริ่มคิดค่าเสื่อม");
            }
            else if (txtEndDateCalDepre.Text == "")
            {
                pFlag = false;
                txtEndDateCalDepre.Focus();
                RadAjaxManager1.Alert("กรุณากรอกวันที่สิ้นสุดการคิดค่าเสื่อม");
            }
            return pFlag;
        }
        private void LoadToObject()
        {
            M_CalPriceAccount info = d_cal_price.GetListFillBySeq(cn.ConvertNullInt(hicm_cal_price_account_seq.Text));
            cmbAssetCode.SelectedValue = info.asset_code.ToString();
            cmbAssetCode.Text = info.asset_name.ToString();
            cmbFiscal.SelectedValue = info.fiscal_period.ToString();
            cmbFiscal.Text = info.fiscal_desc.ToString();
            txtStartDateCalDepre.Text = cn.ConvertStrToStrDate(info.start_date_cal_depreciatiion.ToString());
            txtEndDateCalDepre.Text = cn.ConvertStrToStrDate(info.end_date_cal_depreciation.ToString());
            txtRemark.Text = info.remark.ToString();
        }
    }
}
